﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ria.ApplicationServices;
using CARE.Web.Services;
using CARE.GraphProviderServiceReference;

namespace CARE
{
	public partial class DiseaseSelector : UserControl
    {
        public Type sourceMethod;
        private GraphProviderServiceClient gp;
        private SpreadVizualization spreadVizualization;

        public DiseaseSelector(SpreadVizualization sw)
		{
			// Required to initialize variables
			InitializeComponent();
            gp = new GraphProviderServiceClient();
            gp.GetDiseaseModelGraphCompleted += new EventHandler<GetDiseaseModelGraphCompletedEventArgs>(gp_GetDiseaseModelGraphCompleted);

            spreadVizualization = sw;
		}

        

		private void Open_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            sourceMethod = this.GetType();

            String graphML;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "XML Files (*.xml)|*.xml|GraphML Files (*.graphml)|*.graphml";
            Nullable<Boolean> result = dialog.ShowDialog();
            if (result.Value == true)
            {
                graphML = dialog.File.OpenText().ReadToEnd();
                SimulationParameters.getInstance().DiseaseGraphML = graphML;
                gp.GetDiseaseModelGraphAsync(graphML);                                
            }
		}

        void gp_GetDiseaseModelGraphCompleted(object sender, GetDiseaseModelGraphCompletedEventArgs e)
        {
            SimulationParameters simParameters = SimulationParameters.getInstance();
            simParameters.DiseaseGraph = e.Result as Graph;
            spreadVizualization.EnableNext();
        }

		private void Database_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// TODO: Add event handler implementation here.
            Views.DatabaseDiseaseWindow databasesource = new Views.DatabaseDiseaseWindow();
            databasesource.Closed += new EventHandler(databasesource_Closed);
            databasesource.Show();
		}

        void databasesource_Closed(object sender, EventArgs e)
        {
            var databasesource = sender as Views.DatabaseDiseaseWindow;
            if(databasesource.DialogResult.Value)
            {
                SimulationParameters.getInstance().DiseaseGraphML = databasesource.DiseaseGraphML;
                spreadVizualization.EnableNext();
            }
        }
	}
}
